package com.java.web.session;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.java.web.entity.User;

public class SessionManagement {
	
	/**
	 * 创建新的session
	 * @param request
	 * @return
	 */
	public static HttpSession createNewSession(HttpServletRequest request){
		HttpSession session=request.getSession(false);
		if(session!=null){
			session.invalidate();
		}
		session=request.getSession(true);
		return session;
	}
	
	/**
	 * 将session加入应用上下文
	 * @param request
	 */
	public static void  addSession(HttpServletRequest request){
		HttpSession session=request.getSession(false);
		if(session!=null){
			ServletContext applicationContext=session.getServletContext();
			List<HttpSession> originalSessions=(List<HttpSession>)applicationContext.getAttribute("sessions");
			List<HttpSession> sessions=originalSessions==null?new ArrayList<HttpSession>():originalSessions;
			sessions.add(session);
			applicationContext.setAttribute("sessions", sessions);
		}
	}
	
	/**
	 * 将session加入应用上下文
	 * @param request
	 */
	public static void  addSession(HttpSession session){
		if(session!=null){
			ServletContext applicationContext=session.getServletContext();
			List<HttpSession> originalSessions=(List<HttpSession>)applicationContext.getAttribute("sessions");
			List<HttpSession> sessions=originalSessions==null?new ArrayList<HttpSession>():originalSessions;
			sessions.add(session);
			applicationContext.setAttribute("sessions", sessions);
		}
	}
	
	/**
	 * 将session从应用上下文移除
	 * @param request
	 */
	public static void  removeCurrentSession(HttpServletRequest request){
		HttpSession session=request.getSession(false);
		if(session!=null){
			ServletContext applicationContext=session.getServletContext();
			List<HttpSession> originalSessions=(List<HttpSession>)applicationContext.getAttribute("sessions");
			List<HttpSession> sessions=originalSessions;
			if(sessions!=null){
				sessions.remove(session);
			}
			applicationContext.setAttribute("sessions", sessions);
		}
	}
	
	/**
	 * 将session从应用上下文移除
	 * @param request
	 */
	public static void  removeSession(HttpSession session){
		ServletContext applicationContext=session.getServletContext();
		List<HttpSession> originalSessions=(List<HttpSession>)applicationContext.getAttribute("sessions");
		List<HttpSession> sessions=originalSessions;
		if(sessions!=null){
			sessions.remove(session);
		}
		applicationContext.setAttribute("sessions", sessions);
	}
	
	/**
	 * 获得所有的session
	 * @param request
	 * @return
	 */
	public static List<HttpSession>  getSessions(HttpServletRequest request){
		HttpSession session=request.getSession(false);
		List<HttpSession> originalSessions=null;
		List<HttpSession> sessions=null;
		if(session==null){
			throw new RuntimeException("session不能为null");
		}
		ServletContext applicationContext=session.getServletContext();
		originalSessions=(List<HttpSession>)applicationContext.getAttribute("sessions");
		return originalSessions;
	}
	
	/**
	 * 获得当前用户
	 * @param request
	 * @return
	 */
	public static User  getCurrentUser(HttpServletRequest request){
		HttpSession session=request.getSession(false);
		User user=null;
		if(session!=null){
			user=(User)session.getAttribute("user");
		}
		return user;
		
	}
	
	/**
	 * 根据用户名获得用户对象列表
	 * @param request
	 * @param username
	 * @return
	 */
	public static List<User>  getUser(HttpServletRequest request,String username){
		List<User> userList=new ArrayList<User>();
		List<HttpSession> sessions=getSessions(request);
		if(sessions!=null){
			for(int i=0;i<sessions.size();i++){
				HttpSession session=sessions.get(i);
				User user=(User)session.getAttribute("user");
				if(user!=null){
					if(user.getUsername().equals(username)){
						userList.add(user);
					}
					
				}
			}
		}
		return userList;
		
	}
	
	public static List<HttpSession>  getSessin(HttpServletRequest request,String username){
		List<HttpSession> sessionList=new ArrayList<HttpSession>();
		List<HttpSession> sessions=getSessions(request);
		if(sessions!=null){
			for(int i=0;i<sessions.size();i++){
				HttpSession session=sessions.get(i);
				User user=(User)session.getAttribute("user");
				if(user!=null){
					if(user.getUsername().equals(username)){
						sessionList.add(session);
					}
					
				}
			}
		}
		return sessionList;
		
	}
	
	/**
	 * 将用户加入session中
	 * @param request
	 * @return
	 */
	public static void  addUser(HttpServletRequest request,User user){
		HttpSession session=request.getSession(false);
		if(session==null){
			throw new RuntimeException("session不能为null");
		}
		session.setAttribute("user",user);
	}
	
	

}
